using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Reflection;

/// <summary>
/// Summary description for clsErrorLog
/// </summary>
public class clsErrorLog
{
	public clsErrorLog()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public static void WriteToTextFile()
    {
        // 07/09/2007
        // Code to log the error to come here.
        WriteToTextFile("Ran Code!");
    }
    public static void WriteToTextFile(String Message)
    {
        WriteToTextFile( Message,0);
    }
    public static void WriteToTextFile(String Message,int iErrorLevel)
    {
        WriteToTextFile(Message, "c:\\test\\dunns.log",iErrorLevel);        
    }
    public static void WriteToTextFile(String Message, String FileName, int iErrorLevel)
    {
        // 07/09/2007
        // Code to log the error to come here.
        try
        {
            if (iErrorLevel >= 10)
            {
                System.IO.StreamWriter objSW = new System.IO.StreamWriter(new System.IO.FileStream(FileName, System.IO.FileMode.Append));
                objSW.WriteLine(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
                objSW.Write(Message + "\r\n");
                objSW.WriteLine("-----------------------------------------------------------------------");
                objSW.Close();
            }
        }
        catch (Exception ex)
        {
            // Do nothing - folder doesn't exist 7 this is only used for test purposes anyway.
            // If a developer wants to read the test file, they should make sure it exists.
        }
    }

    public static void LogError(Exception ex,object OriginatingObject,clsUser User, int ErrorLevel)
    {
        try
        {
            // We need to cater for a null user
            String strUserID = "null";
            String strTargetSite = "";
            String strStackTrace = "";
            
            int iErrorLogLevel = int.Parse(clsCommonFunctions.GetAdminSetting(User, "ErrorLogLevel"));
            if (ErrorLevel >= iErrorLogLevel)
            {
                Type objType = OriginatingObject.GetType();
                String strMethod = ex.TargetSite.ToString();
                System.Text.StringBuilder objSBPropertyInfo = new System.Text.StringBuilder();
                foreach (PropertyInfo objPI in objType.GetProperties())
                {
                    try
                    {
                        objSBPropertyInfo.Append(objPI.Name + " = " + objPI.GetValue(OriginatingObject, null).ToString() + "\r\n");
                    }
                    catch (NullReferenceException exc)
                    {
                        objSBPropertyInfo.Append(objPI.Name + " = null\r\n");
                    }
                    catch (Exception exc)
                    {
                        objSBPropertyInfo.Append(objPI.Name + " = ERROR RETRIEVING VALUE\r\n");
                    }
                }
                if (User != null)
                    strUserID = User.iUserID.ToString();
                if (ex.TargetSite != null)
                {
                    strTargetSite = ex.TargetSite.ToString();
                }
                if (ex.StackTrace != null)
                {
                    strStackTrace = ex.StackTrace.ToString();
                }
                DataConnection.GetDataObject().Execute("spErrorLogInsert '" + strTargetSite.ToString() + "','" + strTargetSite + "'," + strUserID + ",'" + ex.Message.Replace("'", "''").Replace("%", "%%") + "','" + strStackTrace.Replace("'", "''").Replace("%", "%%") + "','" + objSBPropertyInfo.ToString().Replace("'", "''").Replace("%", "%%") + "','" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "'");
            }
        }
        catch (Exception exIgnore)
        {
            //Session probably ended - nothing we can do.
            clsErrorLog.WriteToTextFile(exIgnore.Message,"c:\\Newfolder1\\FailedErrors.log",95);
        }
    }
    
}
